package cn.wolfcode.crud.web.controller;

import cn.wolfcode.crud.qo.JsonResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class LoginConcroller {

    @ResponseBody
    @RequestMapping("/logins")
    //d下面是登陆的时候所有拿到的异常
    public JsonResult login(String username, String password){
       //登陆功能要交给shiro去登陆
        try {
            //创建一账户和密码的对象,就是令牌对象
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
            //调用工具类安全登陆
            SecurityUtils.getSubject().login(token);
            return new JsonResult();
        } catch (UnknownAccountException e) {
            return new JsonResult(false, "账号找不到");
        } catch (IncorrectCredentialsException e) {
            return new JsonResult(false, "密码错误啦");
        } catch (AccountException e) {
            return new JsonResult(false, "账号已禁用,请联系管理员");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "登录异常，请联系管理员");
        }
    }


}
